package cn.santi.controller;

import cn.santi.domain.Administrator;
import cn.santi.domain.User;
import cn.santi.domain.adminLog;
import cn.santi.emuns.R;
import cn.santi.jwt.JWTs;
import cn.santi.service.AdminService;
import cn.santi.service.LogService;
import cn.santi.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService as;
    @Autowired
    private LogService ls;
    @Autowired
    private UserService us;
    // 管理员添加
    @GetMapping("/adminAddadmin")
    public R adminAddadmin(Administrator administrator){
        administrator.setId(null);
        administrator.setPosition(0);
        administrator.setLoginCount(0);
        administrator.setTime(null);
        administrator.setState(0);
        Administrator admin =as.getAdminByAccountNumber(administrator.getAccountNumber());
        if (admin != null){
            return R.fail("该账号已存在");
        }else {
            int i = as.adminAddadmin(administrator);
            if (i > 0){
                return R.ok();
            }
            return R.fail("添加失败");
        }
    }
    // 获取管理员列表
    @GetMapping("/adminGetAdminList")
    public R adminGetAdminList(){
        List<Administrator> administrators = as.adminGetAdminList();
        if (administrators.size() > 0){
            return R.ok(administrators);
        }else {
            return R.fail("获取失败");
        }
    }
    // 管理员登录
    @GetMapping("/adminLogin")
    public R adminLogin(String name, String pwd, String ip){
        Administrator administrator = as.adminLogin(name,pwd);
        if (administrator != null){
            if (administrator.getState() == 0){
                String token = JWTs.encryption(administrator.getId() + "");
                administrator.setLoginCount(administrator.getLoginCount()+1);
                // 获取当前时间
                LocalDateTime now = LocalDateTime.now();
                // 定义时间格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                // 格式化当前时间
                String formattedDateTime = now.format(formatter);
                administrator.setTime(formattedDateTime);
                as.adminUpdateAdmin(administrator);
                addlog(token,"系统操作",ip,"登录了系统");
                return R.ok(token);
            }
            return R.fail("该账号已被禁用");
        }else {
            return R.fail("账号或密码错误");
        }
    }
    // 通过token获取管理员信息
    @GetMapping("/adminGetadminByToken")
    public R adminGetadminByToken(String token){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        Administrator administrator = as.getAdminById(idAsInt);
        if (administrator != null){
            return R.ok(administrator);
        }
        return R.fail();
    }
    // 验证密码
    @GetMapping("/adminGetUserByPwd")
    public R adminGetUserByPwd(String token,String pwd){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        Administrator administrator = as.adminGetUserByPwd(idAsInt,pwd);
        if (administrator != null){
            return R.ok();
        }else {
            return R.fail();
        }
    }
    @GetMapping("/adminUpdateAdminPwd")
    public R adminUpdateAdminPwd(String token,String pwd){
        String id = JWTs.decode(token).replace("\"", "");
        int idAsInt = Integer.parseInt(id);
        Administrator administrator = as.getAdminById(idAsInt);
        administrator.setPassword(pwd);
        int i = as.adminUpdateAdminPwd(administrator);
        if (i > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }
    @GetMapping("/adminGetLog")
    public R adminGetLog(){
        List<adminLog> adminLogs = ls.getLog();
        return R.ok(adminLogs);
    }
    @GetMapping("/adminGetLogClassify")
    public R adminGetLogClassify(String classify){
        List<adminLog> adminLogs = null;
        if (classify.equals("全部")){
            adminLogs = ls.getLog();
        }else {
            adminLogs = ls.getLogClassify(classify);
        }
        return R.ok(adminLogs);
    }
    @GetMapping("/adminDeleteLog")
    public R adminDeleteLog(){
        int i = ls.adminDeleteLog();
        if (i > 0){
            return R.ok();
        }
        return R.fail();
    }
    @GetMapping("/adminUpdeteAdminState")
    public R adminUpdeteAdminState(int id){
        Administrator admin = as.getAdminById(id);
        if (admin == null){
            return R.fail();
        }
        if (admin.getState() == 0){
            admin.setState(1);
        } else if (admin.getState()==1){
            admin.setState(0);
        }
        as.adminUpdateAdmin(admin);
        return R.ok();
    }
    @GetMapping("/adminDeleteAdmin")
    public R adminDeleteAdmin(int id){
        int i = as.adminDeleteAdmin(id);
        if (i > 0){
            return R.ok();
        }else {
            return R.fail();
        }
    }
    public void addlog(String token, String OperationClass, String ip, String OperationDetails){
        String id = JWTs.decode(token).replace("\"", ""); // 去除字符串中的所有双引号
        int idAsInt = Integer.parseInt(id); // 将字符串"1"转换为int类型的1
        Administrator administrator = as.getAdminById(idAsInt);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 格式化当前时间
        String formattedDateTime = now.format(formatter);
        administrator.setTime(formattedDateTime);
        as.adminUpdateAdmin(administrator);
        Integer position = administrator.getPosition();
        String identity ="";
        if (position ==1){
            identity="(超管)";
        } else if (position ==0){
            identity="(普通管理员)";
        }
        adminLog adminLog = new adminLog();
        adminLog.setId(null);
        adminLog.setOperationClass(OperationClass);
        adminLog.setOperationPersonnel(administrator.getAccountNumber()+identity);
        adminLog.setOperationTime(formattedDateTime);
        adminLog.setOperationDetails(OperationDetails);
        adminLog.setIp(ip);
        ls.addLog(adminLog);
    }
    @GetMapping("/adminGetadminById")
    public R adminGetadminById(int id){
        Administrator admin = as.getAdminById(id);
        return R.ok(admin);
    }
    @GetMapping("/adminUpdateAdmin")
    public R adminUpdateAdmin(Administrator administrator){
        Administrator administrator1 = new Administrator();
        administrator1.setId(administrator.getId());
        administrator1.setAccountNumber(administrator.getAccountNumber());
        administrator1.setName(administrator.getName());
        administrator1.setPhone(administrator.getPhone());
        if (!administrator.getPassword().equals("")){
            administrator1.setPassword(administrator.getPassword());
        }
        as.adminUpdateAdmin(administrator1);
        return R.ok();
    }
    @GetMapping("/adminGetAdminListByClassify")
    public R adminGetAdminListByClassify(String classify){
        List<Administrator> administrators = as.adminGetAdminListByClassify(classify);
        return R.ok(administrators);
    }
    // 获取管理员信息列表
    @GetMapping("/adminGetUser")
    public R adminGetUser(){
        List<User> User = us.adminGetUser();
        if (User.size()==0){
            return R.fail();
        }else {
            return R.ok(User);
        }
    }

    //修改用户状态
    @GetMapping("/adminUpdateUserState")
    public R adminUpdateUserState(int id,String token,String ip){
        User user = us.getUserByid(id);
        if (user == null){
            return R.fail();
        }
        if (user.getState()==0){
            user.setState(1);
            addlog(token,"用户操作",ip,"禁用了"+user.getNickname());
        }else {
            user.setState(0);
            addlog(token,"用户操作",ip,"启用了"+user.getNickname());
        }
        int i = us.UpdateUserState(user);
        if (i==1) {
            return R.ok();
        }else {
            return R.fail();
        }
    }
    // 根据id获取用户信息
    @GetMapping("/adminGetUserById")
    public R adminGetUserById(int id){
        User user = us.getUserByid(id);
        if (user == null){
            return R.fail();
        }else {
            return R.ok(user);
        }
    }
    // 删除用户
    @GetMapping("/adminDeleteUser")
    public R adminDeleteUser(int id,String token,String ip){
        User user = us.getUserByid(id);
        int i = us.adminDeleteUser(id);
        if (i == 1){
            addlog(token,"用户操作",ip,"删除了"+user.getNickname());
            return R.ok();
        }else {
            return R.fail();
        }
    }
    // 分类获取用户列表
    @GetMapping("/adminGetUserRank")
    public R adminGetUserRank(String Rank,String classify){
        List<User> user =null;
        if (Rank.equals("注册时间") && classify.equals("全部") || Rank.equals("注册时间从早到晚")&& classify.equals("全部") || Rank.equals("注册时间从晚到早")&& classify.equals("全部")){
            user = us.adminGetUserRankAll(Rank);
        } else {
            user = us.adminGetUserRankCondition(Rank,classify);
        }
        if (user == null){
            return R.fail();
        }else {
            return R.ok(user);
        }
    }
}
